package com.hmt.pharmacy.api.result;

import com.hmt.pharmacy.api.result.enums.ResultEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author: huza
 * @createTime: 2024/8/11
 */
@Data
public class R<T> implements Serializable {
    
    private static final long serialVersionUID = 8024997804692659771L;
    
    /**
     * 成功状态
     */
    private boolean success;
    /**
     * 返回信息
     */
    private String message;
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();
    /**
     * 返回数据对象
     */
    private T resultData;
    
    public R() {
    }
    
    public R(ResultEnum resultEnum) {
        this.message = resultEnum.getMessage();
        this.code = resultEnum.getCode();
        this.success = resultEnum.isSuccess();
    }
    
    public R setResultData(T resultData) {
        this.resultData = resultData;
        return this;
    }
    
    public static <T> R<T> ok() {
        return new R<T>(ResultEnum.SUCCESS);
    }
    
    public static <T> R<T> ok(T data) {
        R<T> r = new R<T>(ResultEnum.SUCCESS);
        r.setResultData(data);
        return r;
    }
    
    public static <T> R<T> ok(T data, String message) {
        R<T> r = new R<T>(ResultEnum.SUCCESS);
        r.setResultData(data);
        r.setMessage(message);
        return r;
    }
    
    public static <T> R<T> ok(T data, int code) {
        R<T> r = new R<T>(ResultEnum.SUCCESS);
        r.setResultData(data);
        r.setCode(code);
        return r;
    }
    
    public static <T> R<T> ok(T data, int code, String message) {
        R<T> r = new R<T>();
        r.setSuccess(true);
        r.setResultData(data);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }
    
    public static <T> R<T> fail() {
        return new R<T>(ResultEnum.FAIL);
    }
    
    public static <T> R<T> fail(String message) {
        R<T> r = new R<T>(ResultEnum.FAIL);
        r.setMessage(message);
        return r;
    }
    
    public static <T> R<T> fail(int code, String message) {
        R<T> r = new R<T>(ResultEnum.FAIL);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }
    
    public static <T> R<T> fail(T data, int code, String message) {
        R<T> r = new R<T>();
        r.setSuccess(false);
        r.setResultData(data);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }
    
}
